﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.Financing.Model;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.BaseObject;
using Honestysoft.Workflow.BusinessFacade;
using System.Data.Common;

namespace Honestysoft.Financing.WebUI.Module.Audit
{
    public partial class BatchEdit : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                BindControl();
                Bind();
            }
        }

        private void BindControl()
        {
            ControlHelper.BindEnum(ddlBatchStatus,typeof(EnumItem.BatchStatus));//此处最好改成typeof(EnumItem.XXX)
        }

        private void Bind()
        {
            Bu_AuditBatch obj = null;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj = new BaseBusinessLogic<Bu_AuditBatch>().GetEntity(Request.QueryString["ID"]);
                //txtID.Text = obj.ID;
                numBatchNo.Text = obj.BatchNo.ToString();
                dtpPStartDate.Text = (obj.PStartDate.HasValue ? obj.PStartDate.Value.ToString("yyyy-MM-dd") : "");
                dtpPEndDate.Text = (obj.PEndDate.HasValue ? obj.PEndDate.Value.ToString("yyyy-MM-dd") : "");
                dtpStartDate.Text = (obj.StartDate.HasValue ? obj.StartDate.Value.ToString("yyyy-MM-dd") : "");
                dtpEndDate.Text = (obj.EndDate.HasValue ? obj.EndDate.Value.ToString("yyyy-MM-dd") : "");
                txtRemark.Text = obj.Remark;
                txtCreator.Text = new BaseBusinessLogic<Honestysoft.Baseframework.Model.Sys_User>(Globals.CurrentConnStringName).GetEntity(obj.Creator).UserName;
                hidCreator.Value = obj.Creator;
                //txtCloseUser.Text = obj.CloseUser;
                dtpCreateTime.Text = obj.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
                ddlBatchStatus.SelectedValue = obj.BatchStatus;
            }
            else
            {
                numBatchNo.Text = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).GetNextBatchNo().ToString();
                dtpPStartDate.Text = DateTime.Today.ToString("yyyy-MM-dd");
                dtpPEndDate.Text = DateTime.Today.AddDays(2).ToString("yyyy-MM-dd");

                dtpStartDate.Text = dtpPStartDate.Text;
                dtpEndDate.Text = dtpPEndDate.Text;

                hidCreator.Value = UserLoginer.LoginUser.ID;
                txtCreator.Text = UserLoginer.LoginUser.Name;
                dtpCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ddlBatchStatus.SelectedValue = ((int)EnumItem.BatchStatus.Active).ToString();
            }

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                Edit();
            }
            else
            {
                Add();
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {
            DbTransaction transaction = Honestysoft.BaseLibrary.Data.DatabaseProvider.GetDbTransaction(Globals.CurrentConnStringName);
            DbConnection connection = transaction.Connection;
            try
            {
                WFApply.Drive IDrive = new WFApply.Drive(Globals.CurrentConnStringName);
                Bu_AuditBatch obj = GetSubmitObject();
                new BaseBusinessLogic<Bu_AuditBatch>(Globals.CurrentConnStringName).CreateEntity(obj, transaction);
                //new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).CreateBatch(obj, transaction);
                IDrive.DoStart(UserLoginer.LoginUser.ID, ((int)EnumItem.WFBill.Batch).ToString(), obj.ID, transaction);
                transaction.Commit();
                ScriptHelper.Window.Alert("保存成功！", "window.returnValue='" + obj.ID + "|（" + obj.BatchNo.ToString() + "）第" + ChineseNum.GetChineseNum(obj.BatchNo.ToString()) + "批';window.close()");
            }
            catch (BaseException bex)
            {
                transaction.Rollback();
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        private void Edit()
        {
            try
            {
                Bu_AuditBatch obj = GetSubmitObject();
                new BaseBusinessLogic<Bu_AuditBatch>(Globals.CurrentConnStringName).UpdateEntity(obj);
                //bool ret = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).UpdateBatch(obj);
                //if (ret)
                //{
                    ScriptHelper.Window.Alert("保存成功！", "window.returnValue='" + obj.ID + "|" + ChineseNum.GetChineseNum(obj.BatchNo.ToString()) + "';window.close()");
                //}
                //else
                //{
                //    ScriptHelper.Window.Alert("保存失败！");
                //}
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }
        /// <summary>
        /// 得到提交对象
        /// </summary>
        /// <returns></returns>
        private Bu_AuditBatch GetSubmitObject()
        {
            Bu_AuditBatch obj = new Bu_AuditBatch();
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj.ID = Request.QueryString["ID"];
            }
            else
            {
                obj.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            }
            //obj.ID = txtID.Text;
            if (numBatchNo.Text.Trim().Length > 0) { obj.BatchNo = Convert.ToInt32(numBatchNo.Text); }
            if (dtpPStartDate.Text.Trim().Length > 0) { obj.PStartDate = Convert.ToDateTime(dtpPStartDate.Text); }
            if (dtpPEndDate.Text.Trim().Length > 0) { obj.PEndDate = Convert.ToDateTime(dtpPEndDate.Text); }
            if (dtpStartDate.Text.Trim().Length > 0) { obj.StartDate = Convert.ToDateTime(dtpStartDate.Text); }
            if (dtpEndDate.Text.Trim().Length > 0) { obj.EndDate = Convert.ToDateTime(dtpEndDate.Text); }
            obj.Remark = txtRemark.Text;
            obj.Creator = hidCreator.Value;// txtCreator.Text;
            //obj.CloseUser = txtCloseUser.Text;
            if (dtpCreateTime.Text.Trim().Length > 0) { obj.CreateTime = Convert.ToDateTime(dtpCreateTime.Text); }
            obj.BatchStatus = ddlBatchStatus.SelectedValue;

            return obj;
        }
    }
}